package com.jkoss.wine.investment.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.investment.entity.Handover;
import com.jkoss.wine.investment.entity.Investment;
import com.jkoss.wine.investment.service.IHandoverService;
import com.jkoss.wine.investment.service.IInvestmentService;
import com.jkoss.wine.investment.utils.MyUtils;

 /**
 * 新客户基本情况交接表 前端控制器
 * 
 * @Author ym
 * @Version 1.0, 2019-08-20
 * @See
 * @Since com.jkoss.wine.investment.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/investment/handover")
public class HandoverController extends BaseController {


	@Autowired
	private IHandoverService iHandoverService;
	@Autowired
	private IInvestmentService iInvestmentService;
	
	@RequestMapping("/changeState")
	@RequiresPermissions("/investment/handover/changeState")
	public Object changeState(Handover handover,DwzPageBean dwzPageBean, ModelMap map,HttpServletResponse response) {
		System.out.println("进入changeState------");
		System.out.println(handover.getState());
		Wrapper wrapper = Condition.create();
		if (iHandoverService.updateById(handover)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}
	
	@RequestMapping("/list")
	@RequiresPermissions("/investment/handover/list")
	public String list(String ctno,DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().eq("del", 0);
		if(!CommonUtil.isBlank(ctno)) {
			wrapper.eq("ctno", ctno);
		}
		Page resultPage = iHandoverService.selectPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "investment/handover/list";
	}

	@RequestMapping("/toInsert")
	@RequiresPermissions("/investment/handover/toInsert")
	public Object toInsert(String id,ModelMap map,HttpServletRequest request, HttpServletResponse response) {
		Investment investment = iInvestmentService.selectById(id);
		Handover handover = new Handover();
		handover.setImfid(id);	
		handover.setName(investment.getName());
		handover.setAreaName(investment.getareaName());
		handover.setAreaCode(investment.getareaCode());
		handover.setPhone(investment.getPhone());
		handover.setSource(investment.getSource());
		System.out.println("handover--------------"+handover);
		map.put("record", handover);
		return "investment/handover/edit";
	}

	@RequestMapping("/insert")	
	@RequiresPermissions("/investment/handover/toInsert")
	@ResponseBody
	public Object insert(@Valid Handover handover, HttpServletRequest request, HttpServletResponse response) {
		handover.setState(1);//默认1，申请移交中
		handover.setAddTime(CommonUtil.getDate());//操作时间
		handover.setDel(0);
		handover.setSgtm(CommonUtil.getDate());//签约日期
		handover.setRvid(MyUtils.setOperator());//审核人
		System.out.println("进入insert"+handover);
		Wrapper wrapper1 = Condition.create().eq("id", handover.getImfid()).eq("del", 0);
		Wrapper wrapper = Condition.create().eq("ctno", handover.getCtno()).eq("del", 0);
		//用户已签约一张或一张以上的合同
		if(!CommonUtil.isBlank(iHandoverService.selectList(wrapper1))){
			return ajaxError("客户编号为:"+handover.getImfid()+"已签约一张或一张以上的合同");
		}
		//已存在合同
		if(!CommonUtil.isBlank(handover.selectList(wrapper))){
			return ajaxError("编号为:"+handover.getCtno()+" 的合同已存在");
		}
		if (iHandoverService.insert(handover)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	@RequestMapping("/toUpdate")
	@RequiresPermissions("/investment/handover/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		map.put("record", iHandoverService.selectById(id));
		map.put("upRecord", "");//标记改变状态页面flag
		System.out.println("进入toChangeState------");
		return "investment/handover/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/investment/handover/toUpdate")
	@ResponseBody
	public Object update(@Valid Handover handover, HttpServletRequest request, HttpServletResponse response) {
		if (iHandoverService.updateById(handover)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/delete")
	@RequiresPermissions("/investment/handover/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("id------------------------------"+id[0]);
		List list = new ArrayList();
		for (String string : id) {
			System.out.println(string);
			Handover handover = new Handover();
			handover.setId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			handover.setDel(1);
			list.add(handover);
		}
		System.out.println(list);
		if (iHandoverService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}
	

}

